package com.cdbkj.cloud.pos;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Method;

public class KeyboardActivity extends Activity {

    TextView editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_keyboard);
        init();
    }

    private void init(){
        editText = (TextView)findViewById(R.id.editText1);
        editText.setEnabled(false);

        //editText.addTextChangedListener(new TradeTextWatcher(editText, null));
        TextView[] mBtnkey_digits = new TextView[10];
        for (int i = 0; i < 10; i++) {
            String strid = String.format("btn_price_%d", i);
            mBtnkey_digits[i] = (TextView)findViewById(this
                    .getResources().getIdentifier(strid, "id",
                            this.getPackageName()));
            mBtnkey_digits[i].setOnClickListener(mClickListener);
        }
        TextView mBtnKey_sk = (TextView)findViewById(R.id.btn_price_shoukuan);
        LinearLayout mBtnKey_del = (LinearLayout)findViewById(R.id.btn_price_del);

        mBtnKey_del.setOnClickListener(mClickListener);
        mBtnKey_sk.setOnClickListener(mClickListener);
    }

    private View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int id = view.getId();
            if (id == R.id.btn_price_1
                    || id == R.id.btn_price_2
                    || id == R.id.btn_price_3
                    || id == R.id.btn_price_4
                    || id == R.id.btn_price_5
                    || id == R.id.btn_price_6
                    || id == R.id.btn_price_7
                    || id == R.id.btn_price_8
                    || id == R.id.btn_price_9
                    || id == R.id.btn_price_0) {
                String input = ((TextView) view).getText().toString();
                if (input == null){
                    editText.setText(input);
                }else if (input != null) {
                    String strTmp = editText.getText().toString();
                    strTmp += input;
                    editText.setText(strTmp);
                }
                editText.setTextSize(30);
                editText.setTextColor(Color.BLACK);
            }/*else if (id == R.id.btn_price_point)//点
            {
                String inputa = ((TextView) view).getText().toString();
                if (inputa == null){
                    editText.setText(inputa);
                }else if (inputa != null) {
                    String strTmp = editText.getText().toString();
                    strTmp += inputa;
                    editText.setText(strTmp);
                }
                editText.setTextSize(30);
                editText.setTextColor(Color.BLACK);
            } */else if (id == R.id.btn_price_shoukuan) {//收款
                Toast.makeText(KeyboardActivity.this, "点击了收款", Toast.LENGTH_SHORT).show();
            } else if (id == R.id.btn_price_del) {//清除
                if (editText.getText().length() > 0) {
                    String strTmp = editText.getText().toString();
                    strTmp = strTmp.substring(0, strTmp.length() - 1);
                    editText.setText(strTmp);
                }else {
                    editText.setText("");
                }
                editText.setTextSize(30);
                editText.setTextColor(Color.BLACK);
            }

        }
    };

    public static class TradeTextWatcher implements TextWatcher {

        private EditText mEditText;
//      private TextView mTextView;

        public TradeTextWatcher(EditText edit, TextView text) {
            mEditText = edit;
//          mTextView = text;
        }

        @Override
        public void afterTextChanged(Editable arg0) {

            int len = mEditText.getText().length();

            mEditText.setSelection(len);

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {

        }

        @Override
        public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {

        }

    }
}
